'use strict';
var request = require('request-promise')
var WXBizDataCrypt = require('./WXBizDataCrypt')
const ossClient = require("./oss/index");
const wxMiniClient = require("./wxmini/mini_link");
const hmsPush = require("./hmsPush/index");
const hmsToken = require("./hmsPush/token");

exports.main = async (event, context) => {
  console.log("event=====", event)
  if (event.action) {
    switch (event.action) {
      case "request":
        return requestData(event.request)
      case "wxData" :
        return wxData(event)
      case "alioss-objects":
        return ossClient.listObjects()
        break
      case "alioss-put":
        return ossClient.putObject(event.object, event.buffer)
        break
      case "alioss-delete":
        return ossClient.deleteObject(event.object)
        break
      case "genWxShortLink":
        return wxMiniClient.genWxShortLink()
        break
      case "sendHmsMsg":
        return hmsPush.sendHmsMsg(event.tokenArr,event.desc)
        break
      case "sendHmsMsgBack":
        return hmsPush.sendHmsMsgBack(event.body.pushId)
        break
      case "addHmsToken":
        return hmsToken.addHmsToken(event.token, event.desc)
        break
      case "removeHmsToken":
        return hmsToken.removeHmsToken(event.id)
        break
      case "updateHmsToken":
        return hmsToken.updateHmsToken(event.id, event.token, event.desc)
        break
      case "getHmsTokenList":
        return hmsToken.getHmsTokenList()
        break
      case "getHmsMessageList":
        return hmsToken.getHmsMessageList()
        break
      case "removeHmsMessage":
        return hmsToken.removeHmsMessage(event.id)
        break
      default :
        return getRequestParams(event)
    }
  }
  // 使用url化的接口请求
  let body = event.body
  if (body) {
    if (Object.prototype.toString.call(body).slice(8, -1) == 'String') body = JSON.parse(body)
    switch (body.action) {
      case "sendHmsMsg":
        return hmsPush.sendHmsMsg(body.tokenArr,body.desc)
        break
      case "sendHmsMsgBack":
        return hmsPush.sendHmsMsgBack(body.pushId)
        break
      default:
        return requestData(body)
    }
  }
};

function getRequestParams(event) {
  return event
}

function requestData(config) {
  if (typeof config === 'string') config = JSON.parse(config)
  console.log("requestData", config)
  console.log("requestData===typeof string", typeof config === 'string')
  console.log("requestData-url", config.url)
  // config={
  //   "method": "get",
  //   json: true,
  //   params: {appId: 'APP_4E4E0490D21C44C7A439F847A7744D29'},
  //   "url": "https://67dcfd0c-c143-49dd-81a6-3d6aabd6ef7b.bspapp.com/com-power/com-power"
  // }
  if (config.params) {
    let params = convertJsonParams(config.params)
    config.url = config.url + "?" + params
  }
  console.log("config.url", config.url)
  return new Promise((resolve, reject) => {
    request({...config}).then(html => {
      // console.log("html", html)
      if (typeof (html) == "string")
        html = JSON.parse(html)
      resolve(html); //成功返回数据
    }).catch(err => {
      reject(err); //失败返回数据
    })
  })
}

function convertJsonParams(json) {
  return Object.keys(json).map(key => key + '=' + json[key]).join('&')
}

function wxData(event) {
  let iv = event.iv
  let appId = "wx4526fbd058824a3d"
  let sessionKey = event.sessionKey
  let encryptedData = event.encryptedData
  var pc = new WXBizDataCrypt(appId, sessionKey)
  var data = pc.decryptData(encryptedData, iv)
  console.log('解密后 data: ', data)
  return data
}
